Calculator
def calculator():
print("Welcome to the Python Calculator")
print("Operations: +, -, *, /")
try:
num1 = float(input("Enter the first number: "))
op = input("Enter an operation (+, -, *, /): ")
num2 = float(input("Enter the second number: "))
if op == '+':
result = num1 + num2
elif op == '-':
result = num1 - num2
elif op == '*':
result = num1 * num2
elif op == '/':
if num2 == 0:
result = "Error: Cannot divide by zero."
else:
result = num1 / num2
else:
result = "Error: Invalid operation."
print("Result:", result)
except ValueError:
print("Invalid input. Please enter numeric values.")
# Run the calculator
calculator()
Code output
Enter the first number: 8
Enter an operation (+, -, *, /): *
Enter the second number: 5
Result: 40.0